package com.lagou.server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class Bootstrap {
    private int port=111;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = ResourcesLoad.port;
    }

    public void start() throws IOException, DocumentException {
       // ResourcesLoad.loadServletXml();
        ResourcesLoad.loadServerXml();
        ResourcesLoad.doWebXml();
        ResourcesLoad.doNewInstance();
        int corePoolSize=10;
        int maximumPoolSize=50;
        long keepAliveTime=100;
        TimeUnit unit=TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue=new ArrayBlockingQueue<>(50);
        ThreadFactory threadFactory=Executors.defaultThreadFactory();
        RejectedExecutionHandler handler=new ThreadPoolExecutor.CallerRunsPolicy();
        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory,handler);
        ServerSocket serverSocket=new ServerSocket(ResourcesLoad.port);
        System.out.println("服务器已启动 port："+ResourcesLoad.port);
        //1.0
        /*while(true){
            Socket socket = serverSocket.accept();
            OutputStream outputStream=socket.getOutputStream();
            String data="Hello Minicat!";
            String responsText=HttpProtocoUtil.getHttpHeader200(data.getBytes().length)+data;
            outputStream.write(responsText.getBytes());
            outputStream.close();
        }*/
        //2.0
      /*  while (true){
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            Request request=new Request(inputStream);
            Response response=new Response(socket.getOutputStream());
            response.outPutHtml(request.getUrl());
            socket.close();
        }*/
        //3.0 兼容静态资源 动态请求
       /* while (true){
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            Request request=new Request(inputStream);
            Response response=new Response(socket.getOutputStream());
            if(servletMap.get(request.getUrl())==null){
                response.outPutHtml(request.getUrl());
            }else{
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request,response);
            }

            socket.close();
        }*/
       //4.0  线程池优化
       /* while (true){
            Socket socket = serverSocket.accept();
            RequestProcess requestProcess=new RequestProcess(servletMap,socket);
            requestProcess.start();
        }*/
       //5.0 多线程技术
       /* while (true){
            Socket socket = serverSocket.accept();
            RequestProcess requestProcess=new RequestProcess(ResourcesLoad.servletMap,socket);
           threadPoolExecutor.execute(requestProcess);
        }*/
        //6.0 多线程技术
        while (true){
            Socket socket = serverSocket.accept();
            RequestProcess requestProcess=new RequestProcess(socket);
            threadPoolExecutor.execute(requestProcess);
        }
    }


    //加载
    public static void main(String[] args) throws IOException, DocumentException {
        Bootstrap bootstrap=new Bootstrap();
        bootstrap.start();
    }
}
